﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports WindowsPhone.Recipes.Push.Messages
Imports System.Web.Routing
Imports System.Web.Mvc.Html
Imports System.Text
Imports System.ServiceModel.Activation

Namespace Infrastructure

    Public Module Extensions
        <CLSCompliant(False)> _
        <System.Runtime.CompilerServices.Extension()> _
        Public Function SendAndHandleErrors(ByVal message As PushNotificationMessage, ByVal uri As Uri) As MessageSendResultLight
            Dim result = Nothing
            Try
                Dim sendResult = message.Send(uri)
                result = If(sendResult.NotificationStatus = NotificationStatus.Received, New MessageSendResultLight With {.Status = MessageSendResultLight.Success}, New MessageSendResultLight With {.Status = MessageSendResultLight.Error, .Description = "The notification was not received by the device."})
            Catch exception As Exception
                result = New MessageSendResultLight With {.Status = MessageSendResultLight.Error, .Description = exception.Message}
            End Try

            Return result
        End Function

        <System.Runtime.CompilerServices.Extension()> _
        Public Function MenuItem(ByVal helper As HtmlHelper, ByVal linkText As String, ByVal actionName As String, ByVal controllerName As String) As MvcHtmlString
            Dim li = New TagBuilder("li")
            Dim routeData = helper.ViewContext.RouteData
            Dim currentAction = routeData.GetRequiredString("action")
            Dim currentController = routeData.GetRequiredString("controller")
            If String.Equals(currentAction, actionName, StringComparison.OrdinalIgnoreCase) AndAlso String.Equals(currentController, controllerName, StringComparison.OrdinalIgnoreCase) Then
                li.AddCssClass("selected")
            End If

            li.InnerHtml = helper.ActionLink(linkText, actionName, controllerName).ToHtmlString()
            Return MvcHtmlString.Create(li.ToString())
        End Function


        <System.Runtime.CompilerServices.Extension()> _
        Public Sub AddWcfServiceRoute(ByVal routes As RouteCollection, ByVal dataServiceType As Type, ByVal prefix As String)
            routes.Add(New ServiceRoute(prefix, New AutomaticFormatServiceHostFactory(), dataServiceType))
        End Sub

        <System.Runtime.CompilerServices.Extension()> _
        Public Sub AddWcfServiceRoute(Of TService)(ByVal routes As RouteCollection, ByVal prefix As String)
            AddWcfServiceRoute(routes, GetType(TService), prefix)
        End Sub
    End Module
End Namespace